package com.youzan.cloud.component.biz.member.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.youzan.cloud.component.base.util.core.ObjectUtil;
import com.youzan.cloud.component.biz.member.common.util.OutResultUtil;
import com.youzan.cloud.component.biz.member.dal.dataobject.MemberInformation;
import com.youzan.cloud.component.biz.member.dal.dataobject.MemberRelationship;
import com.youzan.cloud.component.biz.member.dal.mapper.MemberInformationMapper;
import com.youzan.cloud.component.biz.member.dal.mapper.MemberRelationshipMapper;
import com.youzan.cloud.component.biz.member.dto.BaseResult;
import com.youzan.cloud.component.biz.member.dto.LogDTO;
import com.youzan.cloud.component.biz.member.dto.MemberBaseDto;
import com.youzan.cloud.component.biz.member.dto.MemberQueryBusinessInputDto;
import com.youzan.cloud.component.biz.member.youzanopen.api.YzMemberApiService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;

/**
 * @author tangsen233
 * @version 1.0
 * @description: 商家主动推送会员基本信息
 * @date 2021/8/4 15:03
 */
@Service
@Slf4j
public class MemberQueryBusinessService extends AbstractService {

    @Autowired
    MemberRelationshipMapper memberRelationshipMapper;

    @Autowired
    MemberInformationMapper memberInformationMapper;

    @Autowired
    YzMemberApiService yzMemberApiService;

    @Override
    public LogDTO buildLog(Object inputParam) {
        return null;
    }

    @Override
    public BaseResult handleException(Exception e) {
        return OutResultUtil.fail("组件异常!","500",e);
    }

    @Override
    public BaseResult doBiz(MemberBaseDto memberBaseDto) {
        MemberQueryBusinessInputDto memberQueryBusinessInputDto = JSONObject.parseObject(JSONObject.toJSONString(memberBaseDto.getMemberCreateDto()), MemberQueryBusinessInputDto.class);
        if(ObjectUtil.isEmpty(memberQueryBusinessInputDto)){
            return OutResultUtil.fail("参数转换异常","500",null);
        }
            MemberRelationship memberRelationship  = memberRelationshipMapper.selectByAccountAndAccountType(memberQueryBusinessInputDto);
        if(ObjectUtil.isNotNull(memberRelationship)){
            //不为空则做修改
            memberRelationship.setMobile(memberQueryBusinessInputDto.getMobile());
            memberRelationship.setThirdAccountType(memberQueryBusinessInputDto.getThirdAccountType()==null?"1":memberQueryBusinessInputDto.getThirdAccountType());
            memberRelationshipMapper.updateIgnoreNull(memberRelationship);
            MemberInformation memberInformation = memberInformationMapper.selectByYzOpenId(memberRelationship.getYzOpenid());
            if(ObjectUtil.isNotNull(memberInformation)){
                BeanUtils.copyProperties(memberQueryBusinessInputDto,memberInformation);
                memberInformation.setUpdateTime(new Date());
                memberInformation.setYzOpenid(memberRelationship.getYzOpenid());
                memberInformationMapper.updateAllByYzOpenId(memberInformation);
            }else{
                saveMemberInformation(memberQueryBusinessInputDto,memberBaseDto);
            }
            //调用有赞API修改用户
            yzMemberApiService.updateMemberInformation(memberInformation,memberRelationship.getKdtid().toString());
            log.info("同步会员信息成功！！");
            return OutResultUtil.success(memberInformation);
        }
        return OutResultUtil.fail("会员信息不存在","500",null);
    }

    public MemberInformation saveMemberInformation(MemberQueryBusinessInputDto memberQueryBusinessInputDto,MemberBaseDto memberBaseDto){
        MemberInformation memberInformation = new MemberInformation();
        BeanUtils.copyProperties(memberQueryBusinessInputDto,memberInformation);
        memberInformation.setUpdateTime(new Date());
        memberInformation.setYzOpenid(memberBaseDto.getYzOpenId());
        memberInformationMapper.insertIgnoreNull(memberInformation);
        return memberInformation;
    }
}